package com.controller;


import com.bean.ProjectUserBean;
import com.jfinal.core.Controller;
import com.model.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chaiyuan on 2017/5/3.
 */
public class TeacherController extends Controller {

    public void index() {
        render("/index_teacher.jsp");
    }

    public void create() {
    }

    public void view() {
        renderJsp(getPara("page"));
    }

    /**
     * 选题列表
     */
    public void select_list() {
        Teacher teacher = getSessionAttr("user");
        List<Project> list = teacher.getProjects();
        List<ProjectUserBean> pub_list = new ArrayList<ProjectUserBean>();
        for (Project item : list) {
            if(item.getInt("status")==1) {
                List<UserProject> up_list = UserProject.dao.find("select * from user_project where project_id ="+item.getInt("id"));
                for (UserProject up:up_list) {
                    ProjectUserBean pub = new ProjectUserBean();
                    pub.setProject(up.getProject());
                    pub.setStudent(up.getStudent());
                    switch (up.getInt("status")){
                        case 0:
                            pub.setStatus("未选择");
                            break;
                        case 1:
                            pub.setStatus("已选择");
                            break;
                            default:
                                break;
                    }
                    pub_list.add(pub);
                }
            }
        }
        setAttr("list",pub_list);
        render("select_student.jsp");
    }

    /**
     * 确认学生
     */
    public void selected(){
        int stu_id=getParaToInt("stu_id");
        int pro_id =getParaToInt("pro_id");
        List<UserProject> list = UserProject.dao.find("select * from user_project where project_id ="+pro_id);
        for (UserProject up:
             list) {
            up.dao.findById(up.getInt("student_id")).set("status",0).update();
        }

        UserProject.dao.findById(stu_id).set("status",1).update();
        Student.dao.findById(stu_id).set("project_id",pro_id).update();
        redirect("/teacher/select_list");
    }

    /**
     * 审核结果
     */
    public void project_result(){
        Teacher teacher = getSessionAttr("user");
        List<Project> lp = teacher.getProjects();
        setAttr("list",lp);
        render("project_result.jsp");
     }
    /**
     * 评分
     */
    public  void score_list(){
        Teacher tea = getSessionAttr("user");
        List<TeacherGrade> tg_list = tea.getTG();
        List<ProjectUserBean> list = new ArrayList<ProjectUserBean>();
        for (TeacherGrade tg:
             tg_list) {
            ProjectUserBean pub = new ProjectUserBean();
            Project project = tg.getProject();
            pub.setProject(project);
            pub.setStudent(project.getStudent());
            list.add(pub);
        }
        setAttr("list",list);
        render("score_list.jsp");
    }
    public void download(){
        renderFile(Student.dao.findById(getParaToInt("id")).getStr("page"));
    }
    public void score(){
        double score = Double.parseDouble(getPara("score"));
        Teacher teacher = getSessionAttr("user");
        TeacherGrade.dao.findFirst("select * from where project_id="+getParaToInt("pro_id")+" and teacher_id = "+teacher.getInt("tea_id")).set("grade",score).update();
        Student.dao.findById(getParaToInt("stu_id")).set("grade",score).update();
        redirect("/teacher");


    }
}
